<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Suscribe extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	public function index()
	{
		$this->load->helper('form');
		$this->load->library('layout');
		$this->load->library('form_validation');
		$this->load->model('user');
		
		$this->load->model('user');
		$nick = $this->user->getUserNick( $this->session->userdata( 'userid' ) );
		
		
		$data = array('usernick' => $nick);
		if( $nick )	// already logged in
		{	
			$this->layout->view( 'main_page', $data );
			return;
		}
		
		
		$this->form_validation->set_rules(	'nickname', 'Pseudonyme', 
											'required|min_length[3]|max_length[30]|alpha_numeric|callback_nickname_check' );
		$this->form_validation->set_rules('pwd', 'Mot de passe', 'required|min_length[6]|md5');
		$this->form_validation->set_rules('pwdconf', 'Confirmer le mot de passe', 'required|matches[pwd]');
		
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->layout->view('suscribe_form', $data);
		}
		else
		{
			$this->validate();
		}
	}
	
	public function nickname_check( $inNickname )
	{
		if( $this->user->existingUser( $inNickname ) )
		{
			$this->form_validation->set_message('nickname_check', 'Nom d\'utilisateur déjà existant.');
			return false;
		}
		else
		{
			return true;
		}
	}
	
	
	public function validate()
	{
		$data = array();
		
		$data['usernick'] = FALSE;
		
		$data['nickname'] = $this->input->post('nickname');
		
		$data['valid'] = $this->user->addUser( $this->input->post('nickname'), $this->input->post('pwd') );
			
		$this->layout->view('suscribe_valid', $data);
	}
}

/* End of file suscribe.php */
/* Location: ./application/controllers/suscribe.php */